package com.weibo.business.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weibo.common.ValidateUserServlet;
import com.weibo.user.Billing;
import com.weibo.user.service.UserService;
import com.weibo.user.service.imp.UserServiceImp;

public class BillingServlet extends ValidateUserServlet {
	private static Logger logger = Logger.getLogger(BillingServlet.class);

	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {

		request.setAttribute("currentmenu", "账单查询");
		UserService userService = new UserServiceImp();
		String type = request.getParameter("type") == null ? "1" : request
				.getParameter("type");
		String currentPagestr = request.getParameter("currentPage") == null ? "0"
				: request.getParameter("currentPage");
		int itype = Integer.parseInt(type);
		int currentPage = Integer.parseInt(currentPagestr);
		List<Billing> listbilling = null;
		// userService.get
		try {
			switch (itype) {
			case 1:
				listbilling = userService.getBillingList(currentPage);
				if(listbilling==null||listbilling.size()==0){
					listbilling=new ArrayList<Billing>();
				}
				request.setAttribute("listbilling", listbilling);
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_balance.jsp").forward(
						request, response);
				break;
			case 2:
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_spend_record.jsp").forward(
						request, response);
				break;
			case 3:
				listbilling = userService.getChargeRecordList(currentPage);
				if(listbilling==null||listbilling.size()==0){
					listbilling=new ArrayList<Billing>();
				}
				request.setAttribute("listbilling", listbilling);
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_charge_record.jsp").forward(
						request, response);
				break;
			case 4:
				listbilling = userService.getBillingList(currentPage);
				if(listbilling==null||listbilling.size()==0){
					listbilling=new ArrayList<Billing>();
				}
				request.setAttribute("listbilling", listbilling);
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_charge.jsp").forward(request,
						response);
				break;
			case 5:
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_tuibu.jsp").forward(request,
						response);
				break;
			default:
				request.getRequestDispatcher(
						"/WEB-INF/billing/Billing_balance.jsp").forward(
						request, response);
			}
		} catch (ServletException e) {
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		}
	}

}
